$(function(){
    $("#list").mothinDatagrid({
        url:'system/user!query.action',
        sortName: 'name',
        sortOrder: 'asc',
        columns:[[
            {field:'name',title:'姓名',width:40,searchcolumn:"string",sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'sex',title:'性别',width:40,searchcolumn:"dictionary",searchcolumnType:"50bc1f1097df7f499900148b",sortable:true,resizable:false,formatter:function(value){
                if(value){
                    return '<span class="nameDisplay" itemId="'+value+'"></span>';
                }
                return "";
            }},
            {field:'account',title:'登陆账号',searchcolumn:"string",width:40,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'title',title:'头衔',width:40,searchcolumn:"dictionary",searchcolumnType:"50c84750440ae36c07677af8",sortable:true,resizable:false,formatter:function(value){
                if(value){
                    return '<span class="nameDisplay" itemId="'+value+'"></span>';
                }
                return "";
            }},
            {field:'organizationId',title:'所在部门',searchcolumn:"organization",width:60,sortable:true,formatter:function(value){
                if(value){
                    return '<span class="nameDisplay" itemId="'+value+'"></span>';
                }
                return "";
            }},
            {field:'email',title:'Email',width:60,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'state',title:'状态',width:20,sortable:true,formatter:function(value){
                if(value){
                    return '<span class="nameDisplay" itemId="'+value+'"></span>';
                }
                return "";
            }},
            {field:'id',title:'操作',width:0,fixed:true,resizable:false,formatter:function(value,row){
                var data=frame_queryOperation(History.getState().url);
                if(data&&data.rows&&data.rows.length>0){
                    var link="";
                    for(var i=0;i<data.rows.length;i++){
                        link+='<button class="btn btn-link btn-xs" onclick="handle(\''+data.rows[i].id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</button>';
                    }
                    return link;
                }
            }}
        ]]
    },handle);
});

function handle(id,action,name,bizId){
    switch(action){
        case 'system/user!add.action':
            showDialog(action,'添加',800,550,function(){
                $("#list").datagrid("reload");
            });
            break;
        case 'system/user!deleteUser.action':
            showConfirm('确认删除吗？',function(){
                $.ajax({
                    type: 'POST',
                    url: action,
                    data: {"systemUser.id":bizId},
                    success: function(data){
                        if(data.ok){
                            showMessage('删除成功',function(){
                                $("#list").datagrid("reload");
                            });
                        }else{
                            showMessage('删除失败：'+data.message,null,'error');
                        }
                    }
                });
            });
            break;
        case 'system/user!view.action':
            showDialog(action+"?systemUser.id="+bizId,'查看',800,500);
            break;
        case 'system/user!changeState.action':
            showDialog(action+"?systemUser.id="+bizId,'设置用户状态',800,500);
            break;
        case 'system/user!update.action':
            showDialog(action+"?systemUser.id="+bizId,'修改',800,550,function(){
                $("#list").datagrid("reload");
            });
            break;
        case 'system/user!resetpsw.action':
            showConfirm('确认重置密码吗？',function(){
                $.ajax({
                    type: 'POST',
                    url: action,
                    data: {"systemUser.id":bizId},
                    success: function(data){
                        if(data.ok){
                            showMessage('密码重置成功');
                        }else{
                            showMessage('密码重置失败：'+data.message,null,'error');
                        }
                    }
                });
            });
            break;
    }
}